package edu.ctbu.insurance.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import edu.ctbu.insurance.bean.Customer;
import edu.ctbu.insurance.mapper.CustomerMapper;
import edu.ctbu.insurance.service.CustomerService;
import edu.ctbu.insurance.service.impl.CustomerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Wrapper;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author djl
 * @since 2024-06-28
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有客户信息
     * @return 所有客户信息
     */
    @RequestMapping("/getAllCustomer")
    @ResponseBody
    public List<Customer> getAllCustomer(){
        return customerService.list();
    }

    /**
     * 既可以查询所有客户信息，也可以按customer_name和customer_phone模糊查询客户信息
     * @param customerName 客户姓名
     * @param customerPhone 客户电话号码
     * @return 返回对应客户信息List
     */
    @RequestMapping("/getAllCustomerLikeNameAndPhone")
    @ResponseBody
    public List<Customer> getAllCustomerLikeNameAndPhone(String customerName,String customerPhone){
        return customerService.getAllCustomerLikeNameAndPhone(customerName,customerPhone);
    }

    /**
     * 既可以查询所有客户信息，也可以按customer_name和customer_phone模糊查询客户信息,并且实现分页展示
     * @param customerName 客户姓名
     * @param customerPhone 客户电话号码
     * @param pageNum 当前页
     * @param pageSize 每页大小
     * @return 返回对应客户信息List
     */
    @RequestMapping("/getAllCustomerLikeNameAndPhoneAndPageHelper")
    @ResponseBody
    public PageInfo getAllCustomerLikeNameAndPhoneAndPageHelper(String customerName, String customerPhone, int pageNum, int pageSize){
        List<Customer> customerList =
                customerService.getAllCustomerLikeNameAndPhoneAndPageHelper(customerName,customerPhone,pageNum,pageSize);
        return new PageInfo(customerList);
    }

    /**
     * 向数据库t_customer表插入一条客户信息
     * @param customer 一个客户实体
     * @return 成功返回true，失败返回false
     */
    @RequestMapping("/insertCustomer")
    @ResponseBody
    public Boolean insertCustomer(Customer customer){
        //随机生成一个4位数ID
        String code = "";
        Customer byId = null;
        while (true) {
            code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 4);
            byId = customerService.getById(code);
            if(byId == null){
                break;
            }
        }
        customer.setCustomerId(code);
        return customerService.save(customer);
    }

    @RequestMapping("/aa")
    @ResponseBody
    public Customer AA(){
        return customerService.getById(1001);
    }
    /**
     * 根据customer_id修改数据库t_customer表对应客户信息
     * @param customer 一个客户实体
     * @return 成功返回true，失败返回false
     */
    @RequestMapping("/updateCustomer")
    @ResponseBody
    public Boolean updateCustomer(Customer customer){
        return customerService.updateById(customer);
    }

    /**
     * 根据customer_id删除，就是将对应delete_flag设置为0,并同时添加一个删除时间戳
     * @param customerId 客户ID
     * @return 成功返回true，失败返回false
     */
    @RequestMapping("/deleteCustomerById")
    @ResponseBody
    public boolean deleteCustomerById(String customerId){
        return customerService.deleteCustomerById(customerId);
    }


    /**
     * 查询所有软删除客户信息，并按删除时间降序
     * @return 所有软删除客户信息
     */
    @RequestMapping("/getDeletedCustomer")
    @ResponseBody
    public List<Customer> getDeletedCustomer(){
        return customerService.getDeletedCustomer();
    }

    /**
     * 按customer_id恢复客户信息
     * @param customerId
     * @return 成功返回true,失败返回false
     */
    @RequestMapping("/backCustomer")
    @ResponseBody
    public boolean backCustomer(String customerId){
        //修改条件
        UpdateWrapper<Customer> customerUpdateWrapper = new UpdateWrapper<>();
        customerUpdateWrapper
                .set("delete_flag",1)
                .eq("customer_id",customerId)
                .set("delete_date",null);
        return customerService.update(customerUpdateWrapper);
    }

    /**
     * 按id彻底删除客户信息
     * @param id
     * @return 成功返回true,失败返回false
     */
    @RequestMapping("/deleteCustomer")
    @ResponseBody
    public boolean deleteCustomer(int id){
        //删除条件
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return customerService.remove(wrapper);
    }

    /**
     * 根据customerId查找产品名
     * policy查找产品名接口，请勿删除
     * 刘金玉
     */
    @RequestMapping("/findCustomerByCustomerId")
    @ResponseBody
    public Customer findCustomerByCustomerId(String customerId){
        Customer customer = customerService.findCustomerByCustomerId(customerId);
        if(customer==null){
            customer = new Customer();
            customer.setCustomerName("未找到");
            return customer;
        }
        return customer;
    }

}
